<?php
// +----------------------------------------------------------------------
// | 购满街 [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://www.twothink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: YagamiZN
// +----------------------------------------------------------------------

namespace app\merb\controller;
use think\Controller;
use think\Db;

/**
 * 后台模块基类
 */
class Base extends Controller {
    /**
     * 构造方法
     */
    protected $uid;
	public function __construct(){
        parent::__construct();
	    /* 访问控制 */
	    if(empty(session('aid'))){
	        session('aid',null);
	        cookie('aid',null);
	        $this->error("请登陆",url('Login/login'));
	    }
        $this->uid=session('aid');

        //未读消息
        $msgNum=model('MsgNotice')->where('role_type',1)->where('a_id',$this->uid)->where('status',0)->count();
        $this->assign('msgNum',$msgNum);


        $msg_timeout_minute = Db::name('system_config')->field('status,extra')->where(['_key' => 'msg_timeout_minute'])->find();
        if(empty($msg_timeout_minute)||$msg_timeout_minute['status']==0){
            $msgDef=model('MsgNotice')->where('role_type',1)->where('a_id',$this->uid)->where('msg_type',3)->where('status',0)->find();
            $this->assign('msgDef',json_encode($msgDef,JSON_UNESCAPED_UNICODE));
            $msgYou=model('MsgNotice')->where('role_type',1)->where('a_id',$this->uid)->where('msg_type',2)->where('status',0)->find();
            $this->assign('msgYou',json_encode($msgYou,JSON_UNESCAPED_UNICODE));
        }else{
            $selectTime=time()-($msg_timeout_minute['extra']*60);
            $msgDef=model('MsgNotice')->where('role_type',1)->where('a_id',$this->uid)->where('msg_type',3)->where('status',0)->where('create_time','>=',$selectTime)->find();
            $this->assign('msgDef',json_encode($msgDef,JSON_UNESCAPED_UNICODE));
            $msgYou=model('MsgNotice')->where('role_type',1)->where('a_id',$this->uid)->where('msg_type',2)->where('status',0)->where('create_time','>=',$selectTime)->find();
            $this->assign('msgYou',json_encode($msgYou,JSON_UNESCAPED_UNICODE));
        }
//	    /* 权限控制*/
//	    $Auth=new \com\Auth();
//	    $request=  \think\Request::instance();
//	    $c=$request->controller();
//	    $a=$request->action();
//	    $rule=$c.'/'.$a;
//	    //echo $rule;
//	    $noCheck = [
//            'Index/index',
//            'Index/home',
//            'Index/indexmsg',
//            'Index/pickshifu',
//            'Base/getuserinfo',
//            'Base/getxicheinfo',
//            'Base/getstationinfo',
//            'Base/getorderinfo'
//        ];
//	    if(session('aid')!=1){
//	        if(!in_array($rule, $noCheck)){
//        	    if(!$Auth->check($rule, session('aid'))){
//    	           $this->error("未授权访问");
//	           }
//    	    }
//    	    if(empty(session('roleslist'))){
//    	        $role=session('role');
//    	        $rules=db('auth_group')->field(['rules'])->where('id',$role)->find();
//    	        $rulesarr=explode(',', $rules['rules']);
//    	        $ruleslist=db('auth_rule')->field(['id','name','title','pid'])->whereIn('id', $rulesarr)->where(function ($query) {
//                $query->where('level', 1)->whereor('level', 2);
//    	        })->select();
//    	        $ruleslistarr=list_to_tree($ruleslist);
//    	        session('roleslist',serialize($ruleslistarr));
//    	    }
//	    }
        /* 读取站点配置 */
        parent::__construct();
	}

    public function getCityName($id)
    {
        $city = Db::name('city')->where(['id'=>$id])->find();
        return $city['name'];
    }
//	/**
//	 * 构造方法，初始化站点信息
//	 */
//    protected function initialize(){
//        //没有缓存查询mysql设置缓存
//        if(!cache('baseWebMsg')){
//            $baseWebmsg=db('webmsg')->select();
//            cache('baseWebMsg', $baseWebmsg, 3600*24*3);
//        }
//        if(!cache('sysCar')){
//            $carsys=db('sys_car')->select();
//            cache('sysCar', $carsys, 3600*24*3);
//        }
//    }

	/* 空操作，用于输出404页面 */
	public function _empty(){
		$this->redirect('Index/index');
	}

    public function getChannelArray()
    {
        $list = model('Channel')->getListByMap();
        $channels = [];
        foreach ($list as $k=>$v){
            $channels[$v['id']] = $v['name'];
        }
        return $channels;
    }
}
